package cn.itsource.pethome.system.atools.registerAndLogin.controller;

import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.system.atools.registerAndLogin.domain.EmailRegister;
import cn.itsource.pethome.system.atools.registerAndLogin.domain.PhoneRegister;
import cn.itsource.pethome.system.user.domain.User;
import cn.itsource.pethome.system.user.service.IUserService;
import cn.itsource.pethome.utils.AjaxResult;
import cn.itsource.pethome.utils.MobileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.regex.Pattern;

/**
 * @Author:R
 * @Date:2023/1/12 15:49
 * Description 用户注册的接口（手机号和邮箱注册
 */
@RestController
@RequestMapping("/register")
@Api(value = "用户注册的接口（手机号和邮箱注册",description="前台用户注册的接口")
public class RegisterController {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private IUserService userService;

    @PostMapping("/phoneRegister")
    @ApiOperation(value = "用于手机号注册")
    public AjaxResult phoneRegister(@RequestBody PhoneRegister register){
        String phone = register.getPhone();
        String phoneCode = register.getPhoneCode();
        String passwors = register.getPassword();
        String passwordRepeat = register.getPasswordRepeat();
        //数据校验
        if(phone == null || "".equals(phone.trim()))
            return AjaxResult.error().setMsg("手机号不能为空！");
        if(phone.length() != 11)
            return AjaxResult.error().setMsg("手机号应为11位！");
        if(!MobileUtil.checkPhone(phone))
            return AjaxResult.error().setMsg("手机号错误或者非大陆手机号");
        if(phoneCode == null || "".equals(phoneCode.trim()))
            return AjaxResult.error().setMsg("手机验证码不能为空！");
        if(passwors == null || "".equals(passwors.trim()))
            return AjaxResult.error().setMsg("密码不能为空！");
        if(passwordRepeat == null || "".equals(passwordRepeat.trim()))
            return AjaxResult.error().setMsg("确认密码不能为空！");
        if (!passwors.equals(passwordRepeat.trim()))
            return AjaxResult.error().setMsg("两次密码输入不一致！");
        User user = userService.findByPhone(phone);
        if (user != null)
            return AjaxResult.error().setMsg("该手机号已被注册！");
        //取出手机号验证码
        String vcode = (String)redisTemplate.opsForValue().get(phone + BaseConstants.Phone.PHONE_KEY_SUFFIX);
        if (vcode == null)
            return AjaxResult.error().setMsg("验证码已过期，请重新获取！");
        if(!vcode.equals(phoneCode.trim()))
            return AjaxResult.error().setMsg("手机验证码输入错误！");
        userService.add(register);
        return AjaxResult.success().setMsg("注册成功！");
    }

    @PostMapping("/emailRegister")
    @ApiOperation(value = "用于邮箱注册")
    public AjaxResult emailRegister(@RequestBody EmailRegister register){
        String email = register.getAccount();
        String imageCode = register.getImageCode();
        String password = register.getPassword();
        String passwordRepeat = register.getPasswordRepeat();
        String key = register.getKey();
        //数据校验
        if (key == null || "".equals(key.trim()))
            return AjaxResult.error().setMsg("非法访问！");
        if(email == null || "".equals(email.trim()))
            return AjaxResult.error().setMsg("邮箱不能为空！");
        String tegex = "[a-zA-Z0-9]+@[a-zA-Z0-9]+\\\\.[a-zA-Z0-9]+";
        if(Pattern.matches(tegex,email))
            return AjaxResult.error().setMsg("邮箱格式错误！");
        User byEmail = userService.findByEmail(email);
        if (byEmail != null)
            return AjaxResult.error().setMsg("该邮箱已被注册！");
        if(imageCode == null || "".equals(imageCode.trim()))
            return AjaxResult.error().setMsg("验证码不能为空！");
        //图形验证码校验
        String value = (String)redisTemplate.opsForValue().get(key);
        if (value == null)
            return AjaxResult.error().setMsg("图形验证码已过期，请重新获取！");
        if(!value.equals(imageCode.trim()))
            return AjaxResult.error().setMsg("图形验证码输入错误！");

        if(password == null || "".equals(password.trim()))
            return AjaxResult.error().setMsg("密码不能为空！");
        if(passwordRepeat == null || "".equals(passwordRepeat.trim()))
            return AjaxResult.error().setMsg("确认密码不能为空！");
        if (!password.equals(passwordRepeat.trim()))
            return AjaxResult.error().setMsg("两次密码输入不一致！");

        //添加用户
        try {
            userService.add(register);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("邮件发送异常：" + e.getMessage());
            return AjaxResult.error().setMsg("网络异常，请联系管理员");
        }
        return AjaxResult.success();
    }
}
